package com.wyh.controller.feemanagementcontroller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.result.Result;
import com.wyh.domain.dto.feemanagementdto.*;
import com.wyh.domain.dto.gfive.LoginUser;
import com.wyh.domain.po.feemanagementpo.Collectionverifications;
import com.wyh.domain.po.feemanagementpo.PaymentRecords;
import com.wyh.domain.po.gfive.User;
import com.wyh.domain.vo.feemanagementvo.AllPaymentRecordsVo;
import com.wyh.domain.vo.feemanagementvo.BillDetailsVos;
import com.wyh.domain.vo.feemanagementvo.CollectionverificationVo;
import com.wyh.domain.vo.feemanagementvo.PaymentRecordsVos;
import com.wyh.security.gfive.service.TokenService;
import com.wyh.service.feemanagementservice.BillVerificationService;
import com.wyh.service.feemanagementservice.CollectionverificationService;
import com.wyh.service.feemanagementservice.PaymentRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/collectionverification")
@Api(tags = "收款核销")
@Slf4j
public class CollectionverificationController {
    @Autowired
    private CollectionverificationService service;
    @Autowired
    private PaymentRecordsService paymentRecordsService;
    @Autowired
    private BillVerificationService billVerificationService;
    @Autowired
    private TokenService tokenService;
    @PostMapping("/getBillByCondition")
    @ApiOperation(value = "账单列表",notes = "获取用户授权的楼盘账单")
    public Result<CollectionverificationVo> getCollectionverificationByCondition(@Valid @RequestBody CollectionverificationDto collectionverificationDto, HttpServletRequest request){
        try {
            if(tokenService.getLoginUser(request).getUser() == null){
                return Result.error("0","用户信息获取失败");
            }
            LoginUser loginUser = tokenService.getLoginUser(request);
            User user = loginUser.getUser();
            collectionverificationDto.setBillnumber(collectionverificationDto.getBillnumber().trim());
            collectionverificationDto.setCustomername(collectionverificationDto.getCustomername().trim());
            collectionverificationDto.setPropertyname(collectionverificationDto.getPropertyname().trim());
            /*  将数据传输对象Dto的属性拷贝到page中 */
            IPage<Collectionverifications> page = new Page<>(collectionverificationDto.getCurrent(),collectionverificationDto.getSize());
            return Result.success(service.getAllByIdAndPage(page,collectionverificationDto,user.getAccount()));
        } catch (Exception e) {
            log.error("{feemanagementcontroller/CollectionverificationController/collectionverification/getBillByCondition}接口异常");
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/getPayByConditonNotClaim")
    @ApiOperation(value = "未认领收款记录列表",notes = "获取未认领收款记录列表")
    public Result<PaymentRecordsVos> getPaymentRecordsByCondition(@Valid @RequestBody PaymentRecordsDtos paymentRecordsDtos){
        try {
            if((paymentRecordsDtos.getStartAmount().compareTo(paymentRecordsDtos.getEndAmount()))>0){
                return Result.error("条件参数非法","到账区间起金额不能大于止金额");
            }
            if(paymentRecordsDtos.getStartTime() != null && paymentRecordsDtos.getEndTime() != null){
                if((paymentRecordsDtos.getStartTime().getTime() > paymentRecordsDtos.getEndTime().getTime())){
                    return Result.error("条件参数非法","起日期不能超过止日期");
                }
            }
            paymentRecordsDtos.setCollectionnumber(paymentRecordsDtos.getCollectionnumber().trim());
            /*  将数据传输对象Dto的属性拷贝到page中 */
            IPage<PaymentRecords> page = new Page<>(paymentRecordsDtos.getCurrent(), paymentRecordsDtos.getSize());
            return Result.success(paymentRecordsService.getAllPayMentRecords(page, paymentRecordsDtos));
        } catch (Exception e) {
            log.error("{feemanagementcontroller/CollectionverificationController/collectionverification/getPayByConditonNotClaim}接口异常");
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/savePaymentRecords")
    @ApiOperation(value = "添加客户到未认领的收款记录",notes ="添加客户到未认领的收款记录" )
    public Result<String> savePaymentRecords(@RequestBody PaymentRecordsAddDto paymentRecordsAddDto,HttpServletRequest request){
        try {
            if(tokenService.getLoginUser(request).getUser() == null){
                return Result.error("0","用户信息获取失败");
            }
            LoginUser loginUser = tokenService.getLoginUser(request);
            User user = loginUser.getUser();
            if(paymentRecordsAddDto.getPayIdList().length == 0){
                return Result.error("0","请选择记录");
            }
            boolean b = paymentRecordsService.addPaymentRecordsCustomerId(paymentRecordsAddDto,user.getId());
            if(b){
                return Result.success();
            }
            return Result.error("0","认领操作失败,请稍后再试");
        } catch (Exception e) {
            log.error("{feemanagementcontroller/CollectionverificationController/collectionverification/savePaymentRecords}接口异常");
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/revokePaymentRecords")
    @ApiOperation(value = "撤销选中客户认领的收款记录",notes ="撤销选中客户认领的收款记录" )
    public Result<String> revokePaymentRecords(@RequestBody PaymentRecordsRevokeDto paymentRecordsRevokeDto){
        try {
            if(paymentRecordsRevokeDto.getPayIdList().length == 0){
                return Result.error("0","请选择记录");
            }
            boolean b = paymentRecordsService.revokePaymentRecordsClaim(paymentRecordsRevokeDto);
            if(b){
                return Result.success();
            }
            return Result.error("0","认领操作失败,请稍后再试");
        } catch (Exception e) {
            log.error("{feemanagementcontroller/CollectionverificationController/collectionverification/revokePaymentRecords}接口异常");
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/getPayByConditonClaim")
    @ApiOperation(value = "选中客户收款记录列表",notes = "获取选中客户收款记录列表")
    public Result<PaymentRecordsVos> getPaymentRecordsNotClaimByCondition(@Valid @RequestBody PaymentRecordsDtos paymentRecordsDtos,HttpServletRequest request){
        try {
            if((paymentRecordsDtos.getStartAmount().compareTo(paymentRecordsDtos.getEndAmount()))>0){
                return Result.error("条件参数非法","到账区间起金额不能大于止金额");
            }
            if(paymentRecordsDtos.getStartTime() != null && paymentRecordsDtos.getEndTime() != null){
                if((paymentRecordsDtos.getStartTime().getTime() > paymentRecordsDtos.getEndTime().getTime())){
                    return Result.error("条件参数非法","起日期不能超过止日期");
                }
            }
            if(tokenService.getLoginUser(request).getUser() == null){
                return Result.error("0","用户信息获取失败");
            }
            LoginUser loginUser = tokenService.getLoginUser(request);
            User user = loginUser.getUser();
            paymentRecordsDtos.setCollectionnumber(paymentRecordsDtos.getCollectionnumber().trim());
            /*  将数据传输对象Dto的属性拷贝到page中 */
            IPage<PaymentRecords> page = new Page<>(paymentRecordsDtos.getCurrent(), paymentRecordsDtos.getSize());
            return Result.success(paymentRecordsService.getAllClaimPayMentRecords(page, paymentRecordsDtos,user.getAccount()));
        } catch (Exception e) {
            log.error("{feemanagementcontroller/CollectionverificationController/collectionverification/getPayByConditonClaim}接口异常");
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/getAllPaymenRecordsByCustomerId")
    @ApiOperation(value = "获取选中客户剩有余额的收款记录",notes = "获取选中客户所有的剩有余额的收款记录")
    public Result<AllPaymentRecordsVo> getAllPaymenRecordsByCustomerId(@Valid @RequestBody PaymentRecordsCustomerDto paymentRecordsCustomerDto,HttpServletRequest request){
        try {
            if(tokenService.getLoginUser(request).getUser() == null){
                return Result.error("0","用户信息获取失败");
            }
            LoginUser loginUser = tokenService.getLoginUser(request);
            User user = loginUser.getUser();
            IPage<PaymentRecords> page = new Page<>(paymentRecordsCustomerDto.getCurrent(),paymentRecordsCustomerDto.getSize());
            return Result.success(paymentRecordsService.getAllPaymenRecordsByCustomerId(page,paymentRecordsCustomerDto,user.getAccount()));
        } catch (Exception e) {
            log.error("{feemanagementcontroller/CollectionverificationController/collectionverification/getAllPaymenRecordsByCustomerId}接口异常");
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/getBillListByAccountAndCustomerId")
    @ApiOperation(value = "获取详情信息",notes = "获取核销资源详细信息")
    public Result<BillDetailsVos> getBillListByAccountAndCustomerId(@Valid @RequestBody BillVerificationDto billVerificationDto,HttpServletRequest request){
        try {
            if(tokenService.getLoginUser(request).getUser() == null){
                return Result.error("0","用户信息获取失败");
            }
            LoginUser loginUser = tokenService.getLoginUser(request);
            User user = loginUser.getUser();
            BillDetailsVos billDetailsVos = billVerificationService.getBillListByAccountAndCustomerId(billVerificationDto,user.getAccount(),true);
            if(billDetailsVos.getInvoiceHeaderes() == null){
                return Result.error("0","请通知客户提供开票抬头");
            }
            return Result.success(billDetailsVos);
        } catch (Exception e) {
            log.error("{feemanagementcontroller/CollectionverificationController/collectionverification/getBillListByAccountAndCustomerId}接口异常");
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/verificationInformation")
    @ApiOperation(value = "手动核销",notes = "手动核销客户账单")
    public Result<String> verificationInformation(@Valid @RequestBody VerificationDataDto verificationDataDto,HttpServletRequest request){
        try {
            if(tokenService.getLoginUser(request).getUser() == null){
                return Result.error("0","用户信息获取失败");
            }
            Integer id = tokenService.getLoginUser(request).getUser().getId();
            String account = tokenService.getLoginUser(request).getUser().getAccount();
            String message = service.verificationBill(verificationDataDto,id,account);
            return message.equals("核销成功")?Result.success(message):Result.error("0",message);
        } catch (Exception e) {
            log.error("{feemanagementcontroller/CollectionverificationController/collectionverification/verificationInformation}接口异常");
            throw new RuntimeException(e);
        }
    }
}
